<?php
/**
 * ooPHP - object oriented PHP framework
 * @author Kamil Mikolajczyk ( rego (at) gazeta (dot) pl )
 * @version 1.0
 *
 */

 include_once("form.class.php");
 include_once("printer.class.php");
 include_once("element.class.php");
 include_once("html.class.php");

 /**
  *
  */
 class Column{
	var $name = "";
	var $type = "";
	var $pk = false;
	var $g = false;
	var $s = false;
	var $not0 = true;
	
	function Column($name, $type, $pk, $not0, $g, $s)
	{
		$this -> name = $name;
		$this -> type = $type;
		$this -> pk = $pk;
		$this -> g = $g;
		$this -> s = $s;
		$this -> not0 = $not0;
	}
	
	function getName()
	{
		return $this -> name;
	}
	
	function getType()
	{
		return $this -> type;
	}
	
	function isPK()
	{
		return $this -> pk;
	}
	
	function isNN()
	{
		return $this -> not0;
	}
	
	function hasSetter()
	{
		return $this -> s;
	}
	
	function hasGetter()
	{
		return $this -> g;
	}
	
 }
 
 
 
/**
 * ClassGenerator
 * @author Kamil Mikolajczyk ( rego (at) gazeta (dot) pl )
 * @version 1.0
 *
 */ 
 class ClassGenerator
 {
	static $PK = "PK";
	static $NOT0 = "NN";
	
	static $tableName = "table";
	
	static $cols = Array();
	static $pK = Array();
	
	static function parseColumn($props)
	{
		$name = null;
		$type = null;
		$pk = null;
		$not0 = null;
		$g = null;
		$s = null;
		
		foreach($props as $p)
		{
			if(!$name)
				$name = $p;
			else if(!$type)
				$type = $p;
			else if($p == ClassGenerator::$PK){
				$pK[] = $name;
				$pk = true;
			}else if($p == ClassGenerator::$NOT0)
				$not0 = true;
			else if($p == "G")
				$g = true;
			else if($p == "S")
				$s = true;	
		}
		return new Column($name, $type, $pk, $not0, $g, $s);
	}
	
	static function generateSQL($addCharset = true)
	{
		$tableName = ClassGenerator::$tableName;
		$code = "CREATE TABLE $tableName (\n\t";
		$colsCode = "";
		foreach(ClassGenerator::$cols as $col)
		{
			if($colsCode != "" && trim($col -> getName()) != "")
				$colsCode .= ",\n\t";
			$colsCode .= trim($col -> getName())." ".$col -> getType().($col -> isNN() ? " NOT NULL" : "").($col -> isPK() ? " AUTO_INCREMENT" : "");
		}
		$code .= $colsCode."\n)".($addCharset ? ' ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_polish_ci;' : ';');
		return $code;
	}
	
	static function generatePHP()
	{
	
	}
	
	static function generate()
	{
		$code = "SQL :\n";
		$code .= ClassGenerator::generateSQL();
		
		$code .= "\n\n\nPHP :\n";
		$code .= ClassGenerator::generatePHP();
		
		return $code;
	}
	
	static function parseTable()
	{
		if(isset($_GET['formType']))
		{
			$code = null;
			$ft = $_GET['formType'];
			switch($ft){
			case 0:
				if(isset($_GET['code']))
				{
					$input = $_GET['code'];
					$columns = explode(";", $input);
					foreach($columns as $c)
					{
						$props = explode(":", $c);
						ClassGenerator::$cols[] = ClassGenerator::parseColumn($props);
						echo 'c';
					}
				}
				else
					$code = "Brak kodu!";
			break;
			case 1:
				
			break;
			case 2:
			
			break;
			default:
				$code = "Nieprawidlowy typ formularza";
			break;	
			}
			if(!$code)
			{
				$code = ClassGenerator::generate();
			}
			//echo "<textarea>$code</textarea>";
		}	
	
		return $code;
	}
	
	static function render($indents = 0)
	{
		$f1 = new Form("", "GET");
		$tArea = new Element("textarea");
		$tArea -> addAttribute('rows="10"');
		$tArea -> addAttribute('cols="20"');
		$tArea -> addAttribute('name="code"');
		$f1 -> addElement($tArea);
		$f1 -> addInput("hidden", "formType", 'value="0" id="formType"');
		$f1 -> addSubmit("generate");
		$div1 = new Element("div", "form0");
		$div1 -> addElement($f1);
		
		$btnAdd = new Element("button", null, "btnAddRow", "Dodaj");
		$btnAdd -> addAttribute('onclick="add();"');
		$div2 = new Element("div", "form1");
		$div2 -> addElement($btnAdd);
		$f2 = new Form("", "GET");
		for($i = 0; $i < 30; $i++)
		{
			$p = new Element("p", "p$i");
			$p -> addAttribute(' style="display: '.($i==0 ? "block" : "none").';"');
			$label1 = new Element("label", null, 'inputLabel', "name");
			$p -> addElement($label1);
			$p -> addElement( new Input("text", "$i".'_name'));
			$label2 = new Element("label", null, 'inputLabel', "type");
			$p -> addElement($label2);
			$p -> addElement( new Input("text", "$i".'_type'));
			$label3 = new Element("label", null, 'inputLabel', "PK");
			$p -> addElement($label3);
			$p -> addElement( new Input("checkbox", "$i".'_pk'));
			$label4 = new Element("label", null, 'inputLabel', "G");
			$p -> addElement($label4);
			$p -> addElement( new Input("checkbox", "$i".'_g'));
			$label5 = new Element("label", null, 'inputLabel', "S");
			$p -> addElement($label5);
			$p -> addElement( new Input("checkbox", "$i".'_s'));
			$label6 = new Element("label", null, 'inputLabel', "0");
			$p -> addElement($label6);
			$p -> addElement( new Input("checkbox", "$i".'_n'));
			$label7 = new Element("label", null, 'inputLabel', "F1");
			$p -> addElement($label7);
			$p -> addElement( new Input("checkbox", "$i".'_f1'));
			$label8 = new Element("label", null, 'inputLabel', "F2");
			$p -> addElement($label8);
			$p -> addElement( new Input("checkbox", "$i".'_f2'));
			$label9 = new Element("label", null, 'inputLabel', "F3");
			$p -> addElement($label9);
			$p -> addElement( new Input("checkbox", "$i".'_f3'));
			$label10 = new Element("label", null, 'inputLabel', "F4");
			$p -> addElement($label10);
			$p -> addElement( new Input("checkbox", "$i".'_f4'));
			$label11 = new Element("label", null, 'inputLabel', "F5");
			$p -> addElement($label11);
			$p -> addElement( new Input("checkbox", "$i".'_f5'));
			$f2 -> addElement($p);
		}
		$f2 -> addInput("hidden", "formType", 'value="1" id="formType"');
		$f2 -> addSubmit("generate");
		$div2 -> addElement($f2);
		
		$html = new Html();
		$body = new Element("body");
		$btnType = new Element("button", null, "btnFormType", "Typ");
		$btnType -> addAttribute('onclick="change();"');
		$body -> addElement($btnType);
		$body -> addElement($div1);
		$body -> addElement($div2);
		
		$div3 = new Element("div", "code", "code");
		$text = new Element("textarea");
		$text -> addAttribute('rows = "15"');
		$text -> addAttribute('cols = "55"');
		$text -> addText (ClassGenerator::parseTable());
		$div3 -> addElement($text);
		$body -> addElement($div3);
		
		$html -> setBody($body);
		
		$js = "<script type=\"text/javascript\">
\tvar formNum = 0;
\tvar rowsVisible = 1;
\tfunction change()
\t{
\t\tvar newFormNum = formNum+1;
\t\tnewFormNum %= 3;
\t\tformNum = newFormNum;
\t\tfor(var i=0; i<3; i++)
\t\t\t{
\t\t\t\tif(i!=newFormNum)
\t\t\t\t\tdocument.getElementById('form'+i).style.display = 'none';
\t\t\t\telse
\t\t\t\t\tdocument.getElementById('form'+i).style.display = 'inline';
\t\t\t}
\t}
\tfunction add()
\t{
\t\tif(rowsVisible < 30)
\t\t\tdocument.getElementById('p'+rowsVisible).style.display = 'block';
\t\t\trowsVisible++;
\t
\t}
</script>";
		$html -> setPreBodyJS($js);
		
		$js	= "<script type=\"text/javascript\">
\twindow.onload=change();
</script>";
		$html -> setPostBodyJS($js);
		
		$html -> render($indents);
	}
 }
 ClassGenerator::render(0);
 ?>